package com.ruoyi.web.controller.admin.file;


import com.ruoyi.admin.mapper.LlmFileMapper;
import com.ruoyi.common.constant.MinioConstants;
import com.ruoyi.common.constant.UploadTypeConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.LlmFile;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StpSystemUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.min_io.MinIOUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import io.minio.errors.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * 通用上传接口
 */
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileCommonCollection {


    private final LlmFileMapper llmFileMapper;

    /**
     * 这里做一个通用上传接口
     * 所有文件都存入minio 或者sso 中
     * 存入后记录存入数据库然后返回一个数据库记录id 最后写一个通用工具类 能使用id 直接能访问文件
     *
     * @param file 文件上传数组
     * @return 返回状态值
     */
    @PostMapping("/uploadCommon")
    public AjaxResult uploadCommon(@RequestParam(name = "files") MultipartFile[] file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

        List<Map<String, Object>> list = new ArrayList<>();
        for (MultipartFile multipartFile : file) {
            //将文件上传到minio
            LinkedHashMap<String, Object> objectObjectHashMap = new LinkedHashMap<>();
            Map<String, String> stringStringMap =
                    MinIOUtil.putObject(multipartFile, MinioConstants.FILE_UPLOAD);
            //如果没有fileUrl 就代表上传失败
            if (StringUtils.isBlank(stringStringMap.get("fileUrl"))) {
                objectObjectHashMap.put("status", "error");
                objectObjectHashMap.put("fileName", multipartFile.getOriginalFilename());
                objectObjectHashMap.put("fileId", "");
                objectObjectHashMap.put("size", "");
                objectObjectHashMap.put("time", DateUtils.getTime());
                objectObjectHashMap.put("fileUrl", "");
                list.add(objectObjectHashMap);
                continue;
            }
            //上传成功
            LlmFile llmFile = new LlmFile();
            llmFile.setFileName(stringStringMap.get("fileName"));
            llmFile.setFileUrl(stringStringMap.get("fileUrl"));
            llmFile.setFilePath(stringStringMap.get("filePath"));
            llmFile.setId(IdUtils.simpleUUID());
            llmFile.setCreateBy(StpSystemUtil.getSysUser().getUsername());
            llmFile.setCreateTime(DateUtils.getTime());
            llmFile.setFileType(FileUtils.getFileExtension(stringStringMap.get("fileUrl")));
            llmFile.setUploadType(UploadTypeConstants.LOCAL);
            llmFile.setFileSize(multipartFile.getSize());
            llmFileMapper.insert(llmFile);
            objectObjectHashMap.put("status", "success");
            objectObjectHashMap.put("fileName", multipartFile.getOriginalFilename());
            objectObjectHashMap.put("fileId", llmFile.getId());
            objectObjectHashMap.put("size", multipartFile.getSize());
            objectObjectHashMap.put("time", DateUtils.getTime());
            objectObjectHashMap.put("fileUrl", stringStringMap.get("fileUrl"));
            list.add(objectObjectHashMap);
        }

        return AjaxResult.success(list);
    }


}
